import {initState} from './state'
import {compileToFunction} from '../../../compiler/compiler'
import {callHook, mountComponent} from './lifecycle'
import {nextTick} from '../../utils/next-tick'
import {mergeOptions} from "../../utils";

export function initMinxin(Vue) {
  Vue.prototype._init = function (options) {
    const vm = this
    vm.$options = mergeOptions(vm.constructor.options,options); //为了后续扩展方法都拿到option
    // 初始化状态
    callHook(vm,'beforeCreate');
    initState(vm);
    callHook(vm,'created');
    if (vm.$options.el) {
      //将数据挂载到页面
      vm.$mounted(vm.$options.el)
    }
  }
  Vue.prototype.$mounted = function (el) {
    const vm = this
    vm.$el = document.querySelector(el)
    if (!vm.$options.render) {
      if (!vm.$options.template) {
        vm.$options.template = vm.$el.outerHTML
      }
      vm.$options.render = compileToFunction(vm.$options.template)
    }
    mountComponent(vm)
  }
  Vue.prototype.$nextTick = nextTick
}
